/*
 * Copyright (C) 2009 Rafael Fernandes, Jeff Sharkey, http://jsharkey.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.desktop;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.phonebooksharing.android.provider.PhonebookSharing.DesktopWidgets;
import com.phonebooksharing.android.services.DesktopMessageUpdaterService;

/**
 * 
 * @author Jeff Sharkey, http://jsharkey.org/ (Examples)
 * @author Rafael Fernandes (Adapted based upon Sharkey's examples)
 *
 */
public class MessageAppWidgetConfigure extends Activity {
    public static final String[] intervals = new String[] {
        "15 min", "30 min", "1 hour", "2 hours", "3 hours"
    };

    public static final int[] intervalsInMin = new int[] {
        15, 30, 60, (60 * 2), (60 * 3)
    };

    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

     // Read the appWidgetId to configure from the incoming intent
        mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        
        setConfigureResult(Activity.RESULT_CANCELED);
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
            return;
        }
        
        showDialog(0);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        return new AlertDialog.Builder(MessageAppWidgetConfigure.this)
        .setTitle("Select an Update Interval")
        .setItems(intervals, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                int selected = intervalsInMin[which];
                ContentValues cv = new ContentValues();
                cv.put(DesktopWidgets._ID,             mAppWidgetId);
                cv.put(DesktopWidgets.UPDATE_INTERVAL, selected);
                cv.put(DesktopWidgets.IS_CONFIGURED,   1);
                getContentResolver().insert(DesktopWidgets.CONTENT_URI, cv);
                
                // Trigger pushing a widget update to surface
                DesktopMessageUpdaterService.requestUpdate(new int[] {
                    mAppWidgetId
                });
                startService(new Intent(MessageAppWidgetConfigure.this, DesktopMessageUpdaterService.class));
                
                setConfigureResult(Activity.RESULT_OK);
                finish();
            }
        })
        .create();
    }
    
    /**
     * Convenience method to always include {@link #mAppWidgetId} when setting
     * the result {@link Intent}.
     */
    public void setConfigureResult(int resultCode) {
        final Intent data = new Intent();
        data.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(resultCode, data);
    }
}
